using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Catastrophe.AutoSulution.TaskM
{
	public class Case : BaseCase
	{
		/// <summary>
		/// 条件信息实体
		/// </summary>
		public ConditionEntity Entity { get; set; } = new ConditionEntity();


		public Case this[string conInfo, bool conRet, string details = ""]
		{
			get
			{
				this.Entity.ConInfo = conInfo;
				this.Entity.ConRet = conRet;
				this.Entity.Details = details;
				base.Result = conRet;
				return this;
			}
		}

		public static Case operator &(Case case1, Case case2)
		{
			if (case1 == null)
			{
				throw new ArgumentNullException("case1");
			}
			if (case2 == null)
			{
				throw new ArgumentNullException("case2");
			}
			return Case.CreateCase(case1, case2);
		}

		/// <summary>
		/// 重写|运算
		/// </summary>
		/// <param name="case1"></param>
		/// <param name="case2"></param>
		/// <returns></returns>
		public static Case operator |(Case case1, Case case2)
		{
			if (case1 == null)
			{
				throw new ArgumentNullException("case1");
			}
			if (case2 == null)
			{
				throw new ArgumentNullException("case2");
			}
			return Case.CreateCase(case1, case2, LogicOperaterEnum.Or);
		}

		/// <summary>
		/// 完善Case对象
		/// </summary>
		/// <param name="case1"></param>
		/// <param name="case2"></param>
		/// <param name="combineCase">合并以后的</param>
		/// <param name="operater"></param>
		/// <exception cref="T:System.Exception"></exception>
		public static void BuildCase(Case case1, Case case2, Case combineCase, LogicOperaterEnum operater = LogicOperaterEnum.And)
		{
			if (case1 == null)
			{
				throw new ArgumentNullException("case1");
			}
			if (case2 == null)
			{
				throw new ArgumentNullException("case2");
			}
			string text = "";
			text = operater switch
			{
				LogicOperaterEnum.And => "AND", 
				LogicOperaterEnum.Or => "OR", 
				_ => throw new Exception("invalid param operatorFlag"), 
			};
			ConditionEntity entity = combineCase.Entity;
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(4, 3);
			defaultInterpolatedStringHandler.AppendLiteral("(");
			defaultInterpolatedStringHandler.AppendFormatted(case1.Entity.ConInfo);
			defaultInterpolatedStringHandler.AppendLiteral(" ");
			defaultInterpolatedStringHandler.AppendFormatted(text);
			defaultInterpolatedStringHandler.AppendLiteral(" ");
			defaultInterpolatedStringHandler.AppendFormatted(case2.Entity.ConInfo);
			defaultInterpolatedStringHandler.AppendLiteral(")");
			entity.ConInfo = defaultInterpolatedStringHandler.ToStringAndClear();
			string value = (string.IsNullOrWhiteSpace(case1.Entity.Details) ? "" : ("详情:" + case1.Entity.Details));
			string value2 = (string.IsNullOrWhiteSpace(case2.Entity.Details) ? "" : ("详情:" + case2.Entity.Details));
			string value3 = "状态:" + (case1.Entity.ConRet ? "YES" : "NO");
			string value4 = "状态:" + (case2.Entity.ConRet ? "YES" : "NO");
			string value5 = "状态:" + (combineCase.Result ? "YES" : "NO");
			if (string.IsNullOrWhiteSpace(case1.Summary))
			{
				defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(9, 9);
				defaultInterpolatedStringHandler.AppendLiteral("(");
				defaultInterpolatedStringHandler.AppendFormatted(case1.Entity.ConInfo);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(value3);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(value);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(text);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(case2.Entity.ConInfo);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(value4);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(value2);
				defaultInterpolatedStringHandler.AppendLiteral(") ");
				defaultInterpolatedStringHandler.AppendFormatted(value5);
				defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
				combineCase.Summary = defaultInterpolatedStringHandler.ToStringAndClear();
			}
			else
			{
				defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(7, 7);
				defaultInterpolatedStringHandler.AppendLiteral("(");
				defaultInterpolatedStringHandler.AppendFormatted(case1.Summary);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(text);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(case2.Entity.ConInfo);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(value4);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(value2);
				defaultInterpolatedStringHandler.AppendLiteral(") ");
				defaultInterpolatedStringHandler.AppendFormatted(value5);
				defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
				combineCase.Summary = defaultInterpolatedStringHandler.ToStringAndClear();
			}
		}

		/// <summary>
		/// 深复制
		/// </summary>
		/// <returns></returns>
		public Case DeepCopy()
		{
			Case @case = new Case
			{
				Result = base.Result,
				Entity = new ConditionEntity
				{
					ConInfo = this.Entity.ConInfo,
					ConRet = this.Entity.ConRet,
					Details = this.Entity.Details
				}
			};
			@case.CaseList = new List<BaseCase>();
			@case.CaseList.Add(@case);
			string text = "状态:" + (@case.Entity.ConRet ? "YES" : "NO");
			string value = (string.IsNullOrWhiteSpace(@case.Entity.Details) ? "" : ("详情:" + @case.Entity.Details));
			DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(4, 4);
			defaultInterpolatedStringHandler.AppendLiteral("(");
			defaultInterpolatedStringHandler.AppendFormatted(@case.Entity.ConInfo);
			defaultInterpolatedStringHandler.AppendLiteral(" ");
			defaultInterpolatedStringHandler.AppendFormatted(text);
			defaultInterpolatedStringHandler.AppendLiteral(" ");
			defaultInterpolatedStringHandler.AppendFormatted(value);
			defaultInterpolatedStringHandler.AppendLiteral(")");
			defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
			@case.Summary = defaultInterpolatedStringHandler.ToStringAndClear();
			if (@case.Entity.ConRet)
			{
				@case.SuccessInfo = @case.Entity.ConInfo + " " + text;
			}
			else
			{
				defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(2, 3);
				defaultInterpolatedStringHandler.AppendFormatted(@case.Entity.ConInfo);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(text);
				defaultInterpolatedStringHandler.AppendLiteral(" ");
				defaultInterpolatedStringHandler.AppendFormatted(value);
				@case.FailedInfo = defaultInterpolatedStringHandler.ToStringAndClear();
			}
			return @case;
		}

		public static Case CreateCase(Case case1, Case case2, LogicOperaterEnum operater = LogicOperaterEnum.And)
		{
			if (case1 == null)
			{
				throw new ArgumentNullException("case1");
			}
			if (case2 == null)
			{
				throw new ArgumentNullException("case2");
			}
			Case case3 = new Case();
			case3.CaseList = new List<BaseCase>();
			foreach (Case case6 in case1.CaseList)
			{
				case3.CaseList.Add(new Case
				{
					Result = case6.Result,
					Entity = new ConditionEntity
					{
						ConInfo = case6.Entity.ConInfo,
						ConRet = case6.Entity.ConRet,
						Details = case6.Entity.Details
					}
				});
			}
			foreach (Case case7 in case2.CaseList)
			{
				case3.CaseList.Add(new Case
				{
					Result = case7.Result,
					Entity = new ConditionEntity
					{
						ConInfo = case7.Entity.ConInfo,
						ConRet = case7.Entity.ConRet,
						Details = case7.Entity.Details
					}
				});
			}
			switch (operater)
			{
			case LogicOperaterEnum.And:
				case3.Result = case1.Entity.ConRet && case2.Entity.ConRet;
				case3.Entity.ConRet = case1.Entity.ConRet && case2.Entity.ConRet;
				break;
			case LogicOperaterEnum.Or:
				case3.Result = case1.Entity.ConRet || case2.Entity.ConRet;
				case3.Entity.ConRet = case1.Entity.ConRet || case2.Entity.ConRet;
				break;
			default:
				throw new Exception("invalid param operatorFlag");
			}
			Case.BuildCase(case1, case2, case3, operater);
			if (case3.CaseList.Count == 0)
			{
				case3.CaseList.Add(case1);
			}
			if (case2.CaseList.Count == 0)
			{
				case3.CaseList.Add(case2);
			}
			return case3;
		}
	}
}
